<?
/**
	* @return void
	* @param string $root
	* @param string $addparams
	* @desc Tree of structure 
	**/


if(!defined("common_extfunctions")) { define("common_extfunctions", "loaded");
include(ROOT_PATH."usr/system/common_extfunctions.inc.php"); }

function get_tree($root=false, $addparams=false){
	global $http_path, $tree, $env, $sapi_obj;

	// If the DDC parameter QUERY is passed over this CMS application then replace $addparams value with it
	if($sapi_obj->env["this.param.query"]) {
		$addparams = $sapi_obj->env["this.param.query"];
		$sapi_obj->env["this.param.query"] = false;
	}
	if( preg_match("/_BEGINITEM\(/s", $addparams) ) $root =  preg_replace("/^.*?_BEGINITEM\((.*?)\)_.*?$/s", "\\1", $addparams);



	$root=str_replace("//", "/", "/".$root."/");

	// Analize input parameters
	if( preg_match("/_ORDER\(RANDOM\)_/s", $addparams) ) $ORDER = "random";
	if( preg_match("/_ORDER\(NAME,\s?ASC\)_/s", $addparams) ) $ORDER = "nameasc";
	if( preg_match("/_ORDER\(NAME,\s?DESC\)_/s", $addparams) ) $ORDER = "namedesc";
	if( preg_match("/_ORDER\(DATEC,\s?ASC\)_/s", $addparams) ) $ORDER = "datecasc";
	if( preg_match("/_ORDER\(DATEC,\s?DESC\)_/s", $addparams) ) $ORDER = "datecdesc";
	if( preg_match("/_CHILDRENOFCURRENT_/s", $addparams) ) $CHILDREN = true;
	if( preg_match("/_INDENTIONSTR\(/s", $addparams) ) $INDENTIONSTR =  preg_replace("/^.*?_INDENTIONSTR\((.*?)\)_.*?$/s", "\\1", $addparams);
	if( preg_match("/_LEVELRANGE\(/s", $addparams) )  list($SL, $EL) =  split("-", preg_replace("/^.*?_LEVELRANGE\((.*?)\)_.*?$/s", "\\1", $addparams));
	if( preg_match("/_ALLITEMS_/s", $addparams)) $root=false;
	if( preg_match("/_CASCADEMENU_/s", $addparams)) $_CASCADEMENU_=true;
	if( preg_match("/_GETCONTENT_/s", $addparams)) $_GETCONTENT_=true;

	$cnt=0;
	if(!$INDENTIONSTR) $INDENTIONSTR = "  ";
	$stream=array();

	// Looking for the page.ID descriptions
	if($_CASCADEMENU_) {
		foreach ($tree->map as $item) {
			if($sapi_obj->env["page.ID"]==$item["ID"]) {
				$cascade_arr = split("/", preg_replace("/\[\'/", "/", $item["POINTER"]));
				if($cascade_arr) {
					foreach($cascade_arr as $k=>$v) {
						$cascade_arr[$k] = str_replace("']","", $cascade_arr[$k]);
						if(!$cascade_arr[$k]) unset($cascade_arr[$k]);
					}
					foreach($cascade_arr as $k=>$v) {
						$mask_cascade_arr[$k] = $mask_str .= "\[\'".$v."\'\]";
						$mask_cascade_arr[$k] = "/^".$mask_cascade_arr[$k]."/is";
					}
				}
			}
		}
	}


	foreach ($tree->map as $item) {
		@eval("\$ch=\$tree->structure" . $item["POINTER"] . ";");
		if(isset($ch["CHILDREN"])) $item["CHILDREN"]=1; else $item["CHILDREN"]=0;

		if($sapi_obj->env["page.ID"]==$item["ID"]) { $parent_pointer = $item["POINTER"]; $mask_parent_pointer = preg_replace(array("/\[/","/\]/","/\'/"), array("\[","\]","\'"), $item["POINTER"]); }
		if ($item["LEVEL"]!=0 and !isset($item["MASK"]) and ($CHILDREN ? $item["POINTER"] == $parent_pointer."['".$item["VAR"]."']":true) and (($SL OR $EL)?($item["LEVEL"]>=$SL AND $item["LEVEL"]<=$EL):true) and ($_CASCADEMENU_ ? @preg_match($mask_cascade_arr[$item["LEVEL"]], $item["POINTER"]):true) and ($root?(@preg_match("~".$root."~", $item["HREF"])):true) and perm_check($item) AND substr($item["PERMISSIONS"], 2, 1)!=4) {

			$cnt++;
			$stream[$cnt]=$item;
			$stream[$cnt]["spacer"]="";
			// Temp
			if($item["LEVEL"]!=$old_level AND $item["LEVEL"]>1)
			$stream[$cnt]["newlevel"] = true; else $stream[$cnt]["newlevel"] = false;

			$old_level=$item["LEVEL"];
			$stream[$cnt]["LEVEL5"]=($item["LEVEL"]-2)*5;
			$stream[$cnt]["HREF"]=$http_path.substr($item["HREF"], 1, strlen($item["HREF"]));
			$stream[$cnt]["PATH"]=preg_replace("/^\/(.*)\/$/", "\\1", $item["HREF"]);

			if($item["HREF"]) {
				$_args = split("/", $item["HREF"]);
				if(count($_args)>2) {
					$stream[$cnt]["VAR_PARENT"] = $_args[count($_args)-3];
				}
			}

			if($sapi_obj->env["page"]["ID"]==$item["ID"]) $stream[$cnt]["currentpage"]=1; else $stream[$cnt]["currentpage"]=0;
			if (in_array($item["VAR"], $sapi_obj->env["argv"])) $stream[$cnt]["in_route"]=1; else $stream[$cnt]["in_route"]=0;
			for ($i=0; $i<($item["LEVEL"]); $i++) $stream[$cnt]["spacer"].= $INDENTIONSTR;
		}
	}

	if($_GETCONTENT_) {
		if($stream) {
			foreach($stream as $k => $v) {
				if(isset($db)) unset($db);
				$db = new sapi_vdb(ROOT_PATH."usr/xml/content/".$v["ID"].".xml");
				$content = $db->get_data();
				if($content[1]) {
					foreach($content[1] as $inx => $value) {
						if(!isset($stream[$k][$inx])) $stream[$k][$inx]=$value;
					}
				}
				$db->close();

			}
		}
	}


	if($ORDER == "nameasc") usort ($stream, "cmp_nameasc");
	if($ORDER == "namedesc") usort ($stream, "cmp_namedesc");
	if($ORDER == "random") usort ($stream, "cmp_random");

	if($stream) {
		$__counter = 0;
		$sapi_obj->env["this.lenght"] = count($stream);
		foreach ($stream as $key => $fetch) {
			$stream[$key]["counter"] = ++$__counter;
		}
	}
	
	return $stream;
}

function perm_check($page){
        if($page["PERMISSIONS"]!=""){
                if($_SESSION["user"]["LOGIN"]==$page["OWNER"]){
			$pCurrent=substr($page["PERMISSIONS"], 0, 1);
                }else{
                if($_SESSION["user"]["GROUP"]==$page["GROUP"]){
                        $pCurrent=substr($page["PERMISSIONS"], 1, 1);
                } else {
                        $pCurrent=substr($page["PERMISSIONS"], 2, 1);
                        }
                }
                
                $page["pCurrent"] = $pCurrent;
                if($pCurrent>4) return true;
        }
        return false;
}

?>